﻿package com.zouloux.ui 
{
	import com.zouloux.display.DocumentClass;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 * ...
	 * @author ZoulouX
	 */
	public class Slider extends Component
	{
		public var $slider				:GenericButton;
		
		public var $loadingMc			:Sprite;
		
		private var _dragging			:Boolean = false;
		
		private var _maxSize			:Number	= 100;
		
		private var _live				:Boolean = true
		
		
		public function get maxSize():Number { return _maxSize; }
		public function get value():Number { return $slider.x / _maxSize; }
		public function get live():Boolean { return _live; }
		
		
		public function set value(value:Number):void 
		{
			if (!_dragging)
				$slider.x = Math.max(0, Math.min(value * _maxSize, _maxSize));
		}
		public function set live(value:Boolean):void 
		{
			_live = value;
		}
		public function set maxSize(value:Number):void 
		{
			_maxSize = value;
		}
		
		
		public function Slider ()
		{
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		public function init (event:Event = null):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			addEventListener(MouseEvent.MOUSE_DOWN, startDragHandler);
		}
		
		public function startDragHandler (event:MouseEvent = null):void
		{
			_dragging = true;
			DocumentClass.stage.addEventListener(MouseEvent.MOUSE_UP, stopDragHandler);
			DocumentClass.stage.addEventListener(MouseEvent.MOUSE_MOVE, draggingHandler);
			draggingHandler();
		}
		
		public function draggingHandler (event:MouseEvent = null):void
		{
			$slider.x = Math.max(0, Math.min(mouseX, _maxSize));
			dispatchEvent(new Event(Event.CHANGE));
		}
		
		public function stopDragHandler(event:MouseEvent = null):void 
		{
			_dragging = false;
			DocumentClass.stage.removeEventListener(MouseEvent.MOUSE_MOVE, draggingHandler);
			DocumentClass.stage.removeEventListener(MouseEvent.MOUSE_UP, stopDragHandler);
		}
	}
}